package com.tfswue.ershoujiaoyi.controller;

import com.tfswue.ershoujiaoyi.entity.Product;
import com.tfswue.ershoujiaoyi.service.ProductService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品管理", description = "商品的发布、查询、修改、删除等操作")
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Operation(summary = "发布商品", description = "卖家发布二手商品信息")
    @PostMapping("/publish")
    public Product publish(@RequestBody Product product) {
        return productService.publish(product);
    }

    @Operation(summary = "查询商品列表", description = "查询所有已发布的商品")
    @GetMapping("/list")
    public List<Product> list() {
        return productService.list();
    }

    @Operation(summary = "根据ID查询商品", description = "根据商品ID查询商品详细信息")
    @GetMapping("/{id}")
    public Product getById(@Parameter(description = "商品ID", required = true) @PathVariable Long id) {
        return productService.getById(id);
    }

    @Operation(summary = "修改商品信息", description = "根据商品ID修改商品信息")
    @PutMapping("/update")
    public Product update(@RequestBody Product product) {
        return productService.update(product);
    }

    @Operation(summary = "删除商品", description = "根据商品ID删除商品")
    @DeleteMapping("/delete/{id}")
    public String delete(@Parameter(description = "商品ID", required = true) @PathVariable Long id) {
        boolean success = productService.delete(id);
        return success ? "删除成功" : "删除失败";
    }
}
